<?php
/**
 * Created by PhpStorm.
 * User: THINK
 * Date: 2017/8/29
 * Time: 17:03
 */

namespace Admin\Controller;


use Think\Controller;

class NodeController extends Controller
{
    public function index(){
        $node=M('Node');
        $nodeinfo=$node->field('id,title,pid,level')->order('sort asc')->select();
        $arr=getSon($nodeinfo,0);
        $this->assign('arr',$arr);
       // dump($arr);exit();
        $this->display();
    }
    public function del(){
        $role = M('Node');
        $id = I('id');
        $count=$role->count();
        if ($role->delete($id)>0) {
            echo 1;
        }else{
            echo 0;
        }
    }
    public function add(){
        $id=I('id');
        $level=I('level');
        $map=I('post.');
        if(!$_POST['sub']){
            $this->assign('level',$level);
            $this->assign('pid1',$id);
            $this->assign('level1',$level+1);
            $this->display();
        }else{
            $node=M('node');
            $info=$node->add($map);
            if ($info){
                //E('节点添加失败');
                $this->redirect('Node/index', '', 1, '节点添加成功，页面跳转中...');
            }else {
                $this->redirect('Node/add', '',1, '添加失败...');
            }
        }

    }
    public function upd(){
        $id=I('id');
        $level=I('level');
        $map=I('post.');
        if(!$_POST['sub']){
            $node=M('node');
            $nodeinfo=$node->select($id);
            $this->assign('nodeinfo',$nodeinfo);
            $this->assign('id',$id);

            $this->assign('level',$level);
            $this->display();
        }else{
            $node=M('node');
            $info=$node->save($map);
            if ($info){
                $this->redirect('Node/index', '', 1, '节点修改成功，页面跳转中...');
            }else {
                $this->redirect('Node/upd', '',1, '修改失败...');
            }
        }

    }

}